//基础路径
var base = document.getElementById('testScript').getAttribute('base');

var search=new Vue({
    el:"#search",
    data(){
        return {
            key:""
        }
    },
    methods:{
        search:function () {
            if (this.key.length > 0) {
                console.log(this.key);

            }
        }
    }
});

var app = new Vue({
    el: "#app",
    data() {
        return {
            //课程
            items: [],
            //方向
            groups: [],
            //分类
            classics: [],
            //选择的课程分类条件，0为全部
            classifyActive: 0,
            //选择的方向条件，0为全部
            groupActive: 0,
            //选择的难度等级条件，0为全部
            levelActive: 0,
            //选择的排序条件，0：推荐  1：最新  2：最热
            orderByActive: 0,
            //排序字符串 0:weight  1:create_time 2:hits
            orderBy: 'weight',

            footerText: '加载更多课程',
            //查询状态  0：查询中  1：查询成功  2：查询无结果
            loading: 0
        }
    },
    methods: {
        //点击课程
        click: function (id) {
            window.open(base+'/course/info/'+id);
        },
        //加载课程分类
        getClassics: function () {
            var _t = this;
            axios.get(base + "/classify")
                .then(function (response) {
                    console.log(response.data.data);
                    _t.classics = response.data.data;
                    console.log(_t.groups);

                });

        },
        //加载所有课程方向
        getGroups: function () {
            var _t = this;
            axios.get(base + "/group")
                .then(function (response) {
                    console.log(response.data.data);
                    _t.groups = response.data.data;
                    console.log(_t.classics);

                });
        },
        //分类点击
        clickClassify: function (classifyId) {
            if (classifyId !== this.classifyActive) {
                this.classifyActive = classifyId;
                this.searchCourse();
            }
        },
        //方向点击
        clickGroup: function (groupId) {
            if (groupId !== this.groupActive) {
                this.groupActive = groupId;
                this.searchCourse();
            }
        },
        //难度等级点击
        clickLevel: function (level) {
            if (level !== this.levelActive) {
                console.log(level);
                this.levelActive = level;
                this.searchCourse();
            }
        },
        //排序条件
        clickOrderBy: function (orderBy) {
            if (orderBy !== this.orderByActive) {
                console.log(orderBy);
                this.orderByActive = orderBy;
                if (orderBy === 0) {
                    this.orderBy = 'weight';
                } else if (orderBy === 1) {
                    this.orderBy = 'create_time';
                } else if (orderBy === 2) {
                    this.orderBy = 'hits';
                }

                this.searchCourse();
            }
        },
        //查询符合条件的课程
        searchCourse: function () {
            var _t = this;
            this.loading = 0;
            axios.get(base + "/course", {
                params: {
                    classifyId: _t.classifyActive,
                    groupId: _t.groupActive,
                    level: _t.levelActive,
                    orderBy: _t.orderBy
                }
            }).then(function (response) {
                    if (response.data.data.length !== 0) {
                        _t.loading = 1;
                        _t.items = response.data.data;
                        console.log(_t.items);
                    } else {
                        _t.loading = 2;
                    }
                });
        }

    },
    created() {
        this.getClassics();
        this.getGroups();
        this.searchCourse();
    }
});